import api from './api';
import { Product } from './product.service';
import { seckillActivities as mockActivities } from '../mock/data';
import { orders as mockOrders } from '../mock/data';

export interface SeckillActivity {
  id: number;
  name: string;
  description: string;
  startTime: string;
  endTime: string;
  status: 'upcoming' | 'active' | 'ended';
  products: SeckillProduct[];
  createdAt: string;
  updatedAt: string;
}

export interface SeckillProduct {
  id: number;
  productId: number;
  product: Product;
  seckillPrice: number;
  stock: number;
  limitPerUser: number;
  sold: number;
}

export interface SeckillListResponse {
  activities: SeckillActivity[];
  total: number;
  page: number;
  pageSize: number;
}

export interface SeckillQueryParams {
  page?: number;
  pageSize?: number;
  status?: 'upcoming' | 'active' | 'ended';
}

// Mock implementation using local data
export const getSeckillActivities = async (params: SeckillQueryParams = {}): Promise<SeckillListResponse> => {
  // Simulate API delay
  await new Promise(resolve => setTimeout(resolve, 500));
  
  const { page = 1, pageSize = 10, status } = params;
  
  // Filter activities based on status
  let filteredActivities = [...mockActivities];
  
  if (status) {
    filteredActivities = filteredActivities.filter(activity => activity.status === status);
  }
  
  // Calculate pagination
  const total = filteredActivities.length;
  const startIndex = (page - 1) * pageSize;
  const endIndex = startIndex + pageSize;
  const paginatedActivities = filteredActivities.slice(startIndex, endIndex);
  
  return {
    activities: paginatedActivities,
    total,
    page,
    pageSize,
  };
};

export const getSeckillActivityById = async (id: number): Promise<SeckillActivity> => {
  // Simulate API delay
  await new Promise(resolve => setTimeout(resolve, 300));
  
  const activity = mockActivities.find(a => a.id === id);
  
  if (!activity) {
    throw new Error('Seckill activity not found');
  }
  
  return activity;
};

export const getActiveSeckillActivities = async (params: SeckillQueryParams = {}): Promise<SeckillListResponse> => {
  return getSeckillActivities({ ...params, status: 'active' });
};

export const participateSeckill = async (seckillProductId: number): Promise<{ orderId: number }> => {
  // Simulate API delay
  await new Promise(resolve => setTimeout(resolve, 800));
  
  // Find the seckill product
  let seckillProduct: SeckillProduct | undefined;
  
  for (const activity of mockActivities) {
    const product = activity.products.find(p => p.id === seckillProductId);
    if (product) {
      seckillProduct = product;
      break;
    }
  }
  
  if (!seckillProduct) {
    throw new Error('Seckill product not found');
  }
  
  if (seckillProduct.stock <= 0) {
    throw new Error('Product out of stock');
  }
  
  // Create a new order (in a real app, this would be done on the server)
  const newOrderId = mockOrders.length + 1;
  
  return { orderId: newOrderId };
}; 